<template>
  <div class="search-box">
    <div class="location">
      <div class="city" @click="cityClick">{{ currentCity.cityName }}</div>
      <div class="position" @click="positionClick">
        <span class="text">我的位置</span>
        <img src="@/assets/img/home/icon_location.png" alt="" />
      </div>
    </div>

    <div class="date-range section" @click="showClaender = true">
      <div class="start">
        <div class="date">
          <span class="tip">入住</span>
          <span class="time">{{ startDateStr }}</span>
        </div>
      </div>
      <div class="stay">共{{ stayDay }}晚</div>
      <div class="end">
        <div class="date">
          <span class="tip">离店</span>
          <span class="time">{{ endDateStr }}</span>
        </div>
      </div>
    </div>
    <van-calendar
      v-model:show="showClaender"
      type="range"
      color="#ff9854"
      :round="false"
      :show-confirm="false"
      @confirm="onConfirm"
    />

    <div class="section price-counter bottom-gray-line">
      <div class="start">价格不限</div>
      <div class="end">人数不限</div>
    </div>
    <div class="section bottom-gray-line">关键字/位置/民宿名</div>

    <div class="hot-suggests section">
      <template v-for="(item, index) in hotSuggests" :key="index">
        <div
          class="item"
          :style="{
            color: item.tagText.color,
            background: item.tagText.background.color,
          }"
        >
          {{ item.tagText.text }}
        </div>
      </template>
    </div>

    <div class="search-btn section">
      <div class="btn" @click="searchClick">搜索</div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { storeToRefs } from 'pinia'

import useCityStore from '@/stores/modules/city.js'
import useHomeStore from '@/stores/modules/home.js'
import useMainStore from '@/stores/modules/main.js'
import { formatMonthDay, getDiffDate } from '@/utils/formatDate.js'

const router = useRouter();
const cityStore = useCityStore();
const homeStore = useHomeStore();
const mainStore = useMainStore();

// const props = defineProps({
//   hotSuggests: {
//     type: Array,
//     default: () => []
//   }
// })

// city
const { currentCity } = storeToRefs(cityStore);
// date
const { startDate, endDate } = storeToRefs(mainStore);

const startDateStr = computed(() => formatMonthDay(startDate.value));
const endDateStr = computed(() => formatMonthDay(endDate.value));
const stayDay = ref(getDiffDate(startDate.value, endDate.value));

const showClaender = ref(false);
// suggests and categories
const { hotSuggests } = storeToRefs(homeStore);

function cityClick() {
  router.push("/city");
}

function onConfirm(value) {
  const selectStartDate = value[0];
  const selectEndDate = value[1];

  mainStore.startDate = selectStartDate;
  mainStore.endDate = selectEndDate;
  stayDay.value = getDiffDate(selectStartDate, selectEndDate);

  showClaender.value = false;
}

function searchClick() {
  router.push({
    path: '/search',
    query: {
      startDate: startDate.value,
      endDate: endDate.value,
      currentCity: currentCity.value.cityName
    }
  });
}

function positionClick() {
  navigator.geolocation.getCurrentPosition((res) => {
    console.log('获取位置成功', res);
  }, (err) => {
    console.log('获取位置失败', err);
  }, {
    enableHighAccuracy: true,
    timeout: 3000,
    maximumAge: 0
  });
}
</script>

<style lang="less" scoped>
.search-box {
  --van-calendar-popup-height: 100vh;
}

.location {
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 20px;

  .city {
    flex: 1;
    color: #333;
    font-size: 15px;
  }

  .position {
    width: 74px;
    display: flex;
    align-items: center;

    .text {
      color: #666;
      font-size: 12px;
    }

    img {
      margin-left: 5px;
      width: 18px;
      height: 18px;
    }
  }
}

.section {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding: 0 20px;
  color: #999999;
  height: 44px;

  .start {
    flex: 1;
    height: 44px;
    display: flex;
    align-items: center;
  }

  .end {
    min-width: 30%;
    padding-left: 20px;
  }

  .date {
    display: flex;
    flex-direction: column;

    .tip {
      font-size: 12px;
      color: #999999;
    }

    .time {
      margin-top: 3px;
      color: #333333;
      font-size: 15px;
      font-weight: 500;
    }
  }
}

.date-range {
  height: 44px;

  .stay {
    flex: 1;
    text-align: center;
    font-size: 12px;
    color: #666666;
  }
}

.price-counter {
  .start {
    border-right: 1px solid #f8f9f9;
  }
}

.hot-suggests {
  height: auto;
  .item {
    padding: 4px 8px;
    border-radius: 3px;
    margin: 4px;
    font-size: 12px;
    line-height: 1;
  }
}

.search-btn {
  .btn {
    width: 342px;
    height: 38px;
    max-height: 50px;
    font-weight: 500;
    line-height: 38px;
    text-align: center;
    border-radius: 20px;
    color: #ffffff;
    background-image: var(--theme-linear-gradient);
  }
}
</style>